package com.net.xpay.common.service.impl;

import com.net.xpay.common.domain.SiteConfig;
import com.net.xpay.common.manager.SiteConfigManager;
import com.net.xpay.common.validator.SiteConfigValidator;
import com.net.xpay.common.service.SiteConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 86188
 */
@Slf4j
@Service
public class SiteConfigServiceImpl implements SiteConfigService {
    @Autowired
    private SiteConfigManager siteConfigManager;

    @Autowired
    private SiteConfigValidator siteConfigValidator;

    @Override
    public SiteConfig findSiteConfig() {
        List<SiteConfig> siteConfigs = siteConfigManager.listAll();
       if (CollectionUtils.isEmpty(siteConfigs)){
           return null;
       }
        return siteConfigs.get(0);
    }

    @Override
    public void addSiteConfig(SiteConfig siteConfig) {
        siteConfigValidator.validateAddSiteConfig(siteConfig);
        if (siteConfig.getWechatQrCode() == null){
            siteConfig.setWechatQrCode("");
        }
        siteConfigManager.save(siteConfig);
    }

    @Override
    public SiteConfig updateSiteConfig(SiteConfig siteConfig) {
        siteConfigValidator.validateUpdateSiteConfig(siteConfig);
        if (siteConfig.getWechatQrCode() == null){
            siteConfig.setWechatQrCode("");
        }
        siteConfigManager.update(siteConfig);
        return siteConfig;
    }
}
